import { WeaForm, WeaTableNew } from 'comsMobx'
import { action, computed, observable } from 'mobx';
import { message, Modal } from "antd";
import ListAction from './data/listAction';
import API from '../apis/app';
import DATAAPI from '../apis/data';
import { fillinTypes } from "../constants/app";
import { ExcelPageViewStore } from '../stores/task';
import { WeaLocaleProvider } from "ecCom";
import FilterStore from './data/FilterStore';
import ExcelStore from './dataExcel/DataExcelStore'

const getLabel = WeaLocaleProvider.getLabel;

const { TableStore } = WeaTableNew;
export default class DataDesignerStore {
    @observable appid = '';
    @observable tableStore = new TableStore();
    @observable customSearchMap = {};
    @observable forms = [];
    @observable data = [];
    @observable selectedForm = '';
    @observable noData = false;
    @observable fillinType = '';
    @observable currentTableName = '主表';
    @observable currentTable = '0';
    @observable showSearchAd = false;
    @observable advanceHeight = 300;
    @observable searchsAd;
    @observable showSearchDrop;
    @observable dropIcon;
    @observable buttonsDrop;
    @observable buttonsAd;
    @observable searchsDrop;
    @observable conditions;
    @observable searchsBaseValue;
    @observable conditionForm = new WeaForm();
    @observable hide = false;
    @observable slideStore = new ExcelPageViewStore();
    @observable rightLevel = 1;
    @observable downloadPopverVisible = false;
    @observable relatedLayout;
    @observable detailData = {
        comps: null,
        sys: {}
    };

    @observable filterStore = new FilterStore();
    @observable excelStore;
    @observable showAdvance = false;
    @observable appData;

    constructor() {
        ListAction(this);

        this.excelStore = new ExcelStore(this);
        this.onSearch = this.onSearch.bind(this);
        this.search = this.search.bind(this);
        this.quickSearch = this.quickSearch.bind(this);
        this.changeQuickSearchValue = this.changeQuickSearchValue.bind(this);
        this.clearCondition = this.clearCondition.bind(this);
    }
    @action init = (params) => {
        this.params = params;
        this.appid = params.appid;
        // this.conditions = this.getConditions("",params.appid);
        // this.conditionForm.initFormFields(this.conditions);
        // API.info(this.appid).then(this.setApp)
        //this.initConditions();
        API.info(params.appid).then(data => {
            this.rightLevel = data.rightLevel;
            this.fillinType = data.fillinType;
            this.setApp(data);
        });
    }

    @action changeHide = (bool) => {
        this.hide = bool;
    }

    @action onSearch(value) {
        this.initTable();
    }

    @action setShowSearchAd = (bool) => {
        this.showSearchAd = bool;
        this.excelStore.hideFilter()
    }


    @action
    initTable = () => {
        this.filterMap = {};
        DATAAPI.data({
            //type: this.selectedTab,
            data: this.selectedForm.indexOf("_") > -1 ? 0 : 1,
            appid: this.appid,
            quickSearchValue: this.quickSearchValue,
            //...this.conditionForm.getFormParams()
        }).then(this.setData);
    }

    @action
    filterTable = (filterMap) => {
        this.filterMap = filterMap;
        DATAAPI.data({
            data: 1,
            appid: this.appid,
            quickSearchValue: this.quickSearchValue,
            ...this.conditionForm.getFormParams()
        }).then(this.fetchDatas);
    }

    @action
    fetchDatas = (data) => {
        this.tableStore.getDatas(data.datas);
        this.tableStore.filterMap = this.filterMap;
    }

    @action deleteData = () => {
        const _this = this;
        Modal.confirm({
            content: getLabel('4053', '是否确认删除？'),
            onOk() {
                const dataIds = _this.tableStore.selectedRowKeys.join(",");
                let data = {};
                //data = { formid: _this.selectedForm, dataIds: dataIds };
                switch (_this.fillinType) {
                    case fillinTypes.MULTI:
                        data = { formid: _this.selectedForm, dataIds: dataIds };
                        break;
                    case fillinTypes.FREE:
                        data = { appid: _this.appid, dataIds: dataIds };
                        break;
                }
                DATAAPI.delete(data).then(data => {
                    message.success(getLabel('5345', '删除成功！'));
                    _this.reloadData();
                })
            },
        });

    }


    // @action initConditions = () => {
    //     // 初始化
    //     this.conditions = [{
    //         title: getLabel('502852', '常用条件'),
    //         defaultshow: true,
    //         items: []
    //     }];
    // }

    // @action getConditions(fillinType, appid) {

    //     return this.conditions;
    // }


    @action removeConditions = (searchCol) => {

        const dbField = searchCol.dbField;
        this.conditions[0].items.forEach((r, index) => {

            if (r.domkey[0] == dbField) {
                this.conditions[0].items.splice(index, 1);
            }
        })
    }


    @action getSearchItems = () => {

    }

    @action updateConditions = (conditions) => {
        if (!conditions) {
            conditions = [];
        }
        if (conditions.length == 0) {

            this.showAdvance = false;
        } else {
            this.showAdvance = true;
        }
        this.conditions = conditions;
        this.conditionForm = new WeaForm();
        this.conditionForm.initFormFields(conditions);


    }

    // 3+jcsj+162+20759
    @action analyseCondition = (searchCol, options) => {
        const orderkey = searchCol.dbField;

        const title = searchCol.title;

        const transMethodOther = searchCol.transMethodOther;
        const params = transMethodOther.split("+");
        if (params[0] == 1 && params[2] == 10) {
            return this.getPinfenCodition(orderkey, title);
        } else if (params[0] == 4) {
            return this.getYesOrNoCondition(orderkey, title);
        } else if (params[0] == 5) {
            // const fieldId = params[3];
            // console.log(params, params[3])
            // API.getSelectItem(
            //     fieldId
            // ).then((options) => {

            // })
            return this.getSelectCondition(orderkey, title, options)
        } else if (params[0] == 3) {
            return this.getBrowserCondition(orderkey, title, params[2], params[1])
        } else {
            return this.getTextCondition(orderkey, title);
        }

    }
    // 评分
    @action getPinfenCodition = (orderkey, title) => {
        return {
            colSpan: 2,
            fieldcol: 12,
            labelcol: 6,
            conditionType: "CHECKBOX",
            options: [{
                key: "",
                showname: getLabel(332, "全部"),
                selected: true
            }, {
                key: "0",
                showname: '1星'
            }, {
                key: "1",
                showname: '1星'
            }, {
                key: "2",
                showname: '2星'
            }, {
                key: "3",
                showname: '3星'
            }, {
                key: "4",
                showname: '4星'
            }, {
                key: "5",
                showname: '5星'
            }
            ],
            domkey: [
                orderkey,
            ],
            label: title,
            viewAttr: 2
        }
    }

    // 文本
    @action getTextCondition = (orderkey, title) => {
        return {
            colSpan: 2,
            conditionType: "INPUT",
            domkey: [
                orderkey
            ],
            fieldcol: 12,
            label: title,
            labelcol: 6,
            value: "",
            viewAttr: 2,
            hide: this.fillinType !== fillinTypes.FREE
        }
    }

    @action getYesOrNoCondition = (orderkey, title) => {
        return {
            colSpan: 2,
            fieldcol: 12,
            labelcol: 6,
            conditionType: "SELECT",
            options: [{
                key: "",
                showname: getLabel(332, "全部"),
                selected: true
            }, {
                key: "1",
                showname: '是'
            }, {
                key: "0",
                showname: '否'
            }],
            domkey: [
                orderkey,
            ],
            label: title,
            viewAttr: 2
        }
    }

    @action getSelectCondition = (orderkey, title, options) => {
        return {
            colSpan: 2,
            fieldcol: 12,
            labelcol: 6,
            conditionType: "SELECT",
            options: options,
            domkey: [
                orderkey,
            ],
            label: title,
            viewAttr: 2
        }
    }

    // 3+jcsj+162+20759
    @action getBrowserCondition = (orderkey, title, type, dataValue) => {

        // 由于单选都需要多选，所以需要进行类型转换
        if (type == 1)
            type = 17;
        if (type == 4)
            type = 57;
        if (type == 7)
            type = 18;
        if (type == 8)
            type = 135;
        if (type == 9)
            type = 37;
        if (type == 87)
            type = 184;
        if (type == 161)
            type = 162;
        if (type == 164)
            type = 194;
        if (type == 165)
            type = 166;
        if (type == 167)
            type = 168;
        if (type == 169)
            type = 170;
        if (type == 256)
            type = 257;
        if (type == '161' || type == '162' || type == '256' || type == '257') {
            this.dataParams = { ...this.dataParams, type: dataValue };
        } else {
            this.dataParams = {};
        }
        if (type == '4' || type == '57' || type == '167' || type == '168') {
            this.tabs = {
                tabs: [{
                    "key": "2",
                    "name": getLabel('505968', '组织结构'),
                    "dataURL": null,
                    "dataParams": null,
                    "isSearch": false,
                    "showOrder": 0,
                    "selected": false
                }, {
                    "key": "1",
                    "name": getLabel('505969', '按列表'),
                    "dataURL": null,
                    "dataParams": {
                        "list": "1"
                    },
                    "isSearch": true,
                    "showOrder": 0,
                    "selected": false
                }]
            };
        } else if (type == '164' || type == '169' || type == '170' || type == '194') {
            this.tabs = {
                tabs: [{
                    "key": "2",
                    "name": getLabel('505968', '组织结构'),
                    "dataURL": null,
                    "dataParams": null,
                    "isSearch": false,
                    "showOrder": 0,
                    "selected": false
                }, {
                    "key": "1",
                    "name": getLabel('505969', '按列表'),
                    "dataURL": null,
                    "dataParams": {
                        "list": "1"
                    },
                    "isSearch": true,
                    "showOrder": 0,
                    "selected": false
                }]
            };
        } else {
            this.tabs = {};
        }

        return {
            colSpan: 2,
            conditionType: "BROWSER",
            domkey: [
                orderkey
            ],
            fieldcol: 12,
            label: title,
            labelcol: 6,
            value: "",
            viewAttr: 2,
            browserConditionParam: {
                completeParams: {},
                conditionDataParams: {},
                dataParams: this.dataParams || {},
                destDataParams: this.dataParams || {},
                hasAddBtn: false,
                hasAdvanceSerach: true,
                idSeparator: ",",
                isAutoComplete: 1,
                isDetail: 0,
                isMultCheckbox: false,
                isSingle: false,
                pageSize: 10,
                quickSearchName: "",
                replaceDatas: [],
                title: '浏览框',
                type: type,
                viewAttr: 2,
                ...this.tabs
            },
        }
    }

    @computed get quickSearchValue() {
        if (this.conditionForm.isFormInit && this.conditionForm.has('TBR')) {
            return this.conditionForm.$('TBR').values();
        }
        return '';
    }

    @action changeQuickSearchValue(value) {
        if (this.conditionForm.isFormInit) {
            this.conditionForm.updateFields({
                TBR: { value }
            });
        }
    }
    @action quickSearch() {
        this.initTable();
    }


    @action search() {
        this.excelStore.onFilter();
        this.setShowSearchAd(false);
    }

    @action clearCondition() {
        this.conditionForm.resetForm()
    }

    @action
    fetchDetailData = (dataid, formid) => {
        this.detailData = {
            comps: null,
            sys: {}
        };

        DATAAPI.detailData(dataid, formid).then(res => {
            this.detailData = res;
        });
    }

    @action
    showContent = (bool) => {
        this.downloadPopverVisible = bool;
        if(bool) {

            this.excelStore.filterVisible = false;
        }
    }
}